﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class NhanVienBO : Csla.BusinessBase<NhanVienBO>
    {
        #region Business Properties

        private int? _NhanVienID = null;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; PropertyHasChanged("NhanVienID"); }
        }

        private string _MaNhanVien = null;
        public string MaNhanVien
        {
            get { return this._MaNhanVien; }
            set { this._MaNhanVien = value; PropertyHasChanged("MaNhanVien"); }
        }

        private byte[] _Anh = null;
        public byte[] Anh
        {
            get { return this._Anh; }
            set { this._Anh = value; PropertyHasChanged("Anh"); }
        }

        private string _Ho = null;
        public string Ho
        {
            get { return this._Ho; }
            set { this._Ho = StaticUtil.RebuiltHoTen(value); PropertyHasChanged("Ho"); }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = StaticUtil.RebuiltHoTen(value); PropertyHasChanged("Ten"); }
        }

        private bool? _Nu = false;
        public bool? Nu
        {
            get { return this._Nu; }
            set { this._Nu = value; PropertyHasChanged("Nu"); }
        }

        private DateTime? _NgaySinh = null;
        public DateTime? NgaySinh
        {
            get { return this._NgaySinh; }
            set { this._NgaySinh = value; PropertyHasChanged("NgaySinh"); }
        }

        private string _NoiSinh = null;
        public string NoiSinh
        {
            get { return this._NoiSinh; }
            set { this._NoiSinh = value; PropertyHasChanged("NoiSinh"); }
        }

        private string _DiaChi = null;
        public string DiaChi
        {
            get { return this._DiaChi; }
            set { this._DiaChi = value; PropertyHasChanged("DiaChi"); }
        }

        private string _NguyenQuan = null;
        public string NguyenQuan
        {
            get { return this._NguyenQuan; }
            set { this._NguyenQuan = value; PropertyHasChanged("NguyenQuan"); }
        }

        private string _SoCMND = null;
        public string SoCMND
        {
            get { return this._SoCMND; }
            set { this._SoCMND = value; PropertyHasChanged("SoCMND"); }
        }

        private DateTime? _NgayCapCMND = null;
        public DateTime? NgayCapCMND
        {
            get { return this._NgayCapCMND; }
            set { this._NgayCapCMND = value; PropertyHasChanged("NgayCapCMND"); }
        }

        private string _NoiCapCMND = null;
        public string NoiCapCMND
        {
            get { return this._NoiCapCMND; }
            set { this._NoiCapCMND = value; PropertyHasChanged("NoiCapCMND"); }
        }

        private string _SoHoChieu = null;
        public string SoHoChieu
        {
            get { return this._SoHoChieu; }
            set { this._SoHoChieu = value; PropertyHasChanged("SoHoChieu"); }
        }

        private DateTime? _NgayCapHoChieu = null;
        public DateTime? NgayCapHoChieu
        {
            get { return this._NgayCapHoChieu; }
            set { this._NgayCapHoChieu = value; PropertyHasChanged("NgayCapHoChieu"); }
        }

        private DateTime? _NgayHetHanHoChieu = null;
        public DateTime? NgayHetHanHoChieu
        {
            get { return this._NgayHetHanHoChieu; }
            set { this._NgayHetHanHoChieu = value; PropertyHasChanged("NgayHetHanHoChieu"); }
        }

        private string _Email = null;
        public string Email
        {
            get { return this._Email; }
            set { this._Email = value; PropertyHasChanged("Email"); }
        }

        private string _DienThoai = null;
        public string DienThoai
        {
            get { return this._DienThoai; }
            set { this._DienThoai = value; PropertyHasChanged("DienThoai"); }
        }

        private int? _DanTocID = null;
        public int? DanTocID
        {
            get { return this._DanTocID; }
            set { this._DanTocID = value; PropertyHasChanged("DanTocID"); }
        }

        private int? _TonGiaoID = null;
        public int? TonGiaoID
        {
            get { return this._TonGiaoID; }
            set { this._TonGiaoID = value; PropertyHasChanged("TonGiaoID"); }
        }

        private int? _QuocTichID = null;
        public int? QuocTichID
        {
            get { return this._QuocTichID; }
            set { this._QuocTichID = value; PropertyHasChanged("QuocTichID"); }
        }

        private int? _TrinhDoHocVanID = null;
        public int? TrinhDoHocVanID
        {
            get { return this._TrinhDoHocVanID; }
            set { this._TrinhDoHocVanID = value; PropertyHasChanged("TrinhDoHocVanID"); }
        }

        private int? _TrinhDoNgoaiNguID = null;
        public int? TrinhDoNgoaiNguID
        {
            get { return this._TrinhDoNgoaiNguID; }
            set { this._TrinhDoNgoaiNguID = value; PropertyHasChanged("TrinhDoNgoaiNguID"); }
        }

        private int? _TrinhDoTinHocID = null;
        public int? TrinhDoTinHocID
        {
            get { return this._TrinhDoTinHocID; }
            set { this._TrinhDoTinHocID = value; PropertyHasChanged("TrinhDoTinHocID"); }
        }

        private int? _BangCapID = null;
        public int? BangCapID
        {
            get { return this._BangCapID; }
            set { this._BangCapID = value; PropertyHasChanged("BangCapID"); }
        }

        private int? _ChuyenMonID = null;
        public int? ChuyenMonID
        {
            get { return this._ChuyenMonID; }
            set { this._ChuyenMonID = value; PropertyHasChanged("ChuyenMonID"); }
        }

        private string _NoiDaoTao = null;
        public string NoiDaoTao
        {
            get { return this._NoiDaoTao; }
            set { this._NoiDaoTao = value; PropertyHasChanged("NoiDaoTao"); }
        }

        private int? _PhongBanID = Session.PhongBanID_NhanVien;
        public int? PhongBanID
        {
            get { return this._PhongBanID; }
            set { this._PhongBanID = value; PropertyHasChanged("PhongBanID"); }
        }

        private int? _ChucVuID = Session.MacDinh_ChucVu;
        public int? ChucVuID
        {
            get { return this._ChucVuID; }
            set { this._ChucVuID = value; PropertyHasChanged("ChucVuID"); }
        }

        private int? _TinhTrangCongViecID = 1;
        public int? TinhTrangCongViecID
        {
            get { return this._TinhTrangCongViecID; }
            set { this._TinhTrangCongViecID = value; PropertyHasChanged("TinhTrangCongViecID"); }
        }

        private DateTime? _NgayBatDauLamViec = null;
        public DateTime? NgayBatDauLamViec
        {
            get { return this._NgayBatDauLamViec; }
            set { this._NgayBatDauLamViec = value; PropertyHasChanged("NgayBatDauLamViec"); }
        }

        private DateTime? _NgayKetThucLamViec = null;
        public DateTime? NgayKetThucLamViec
        {
            get { return this._NgayKetThucLamViec; }
            set { this._NgayKetThucLamViec = value; PropertyHasChanged("NgayKetThucLamViec"); }
        }

        private string _MaHopDongLaoDong = null;
        public string MaHopDongLaoDong
        {
            get { return this._MaHopDongLaoDong; }
            set { this._MaHopDongLaoDong = value; PropertyHasChanged("MaHopDongLaoDong"); }
        }

        private int? _LoaiHopDongLaoDongID = null;
        public int? LoaiHopDongLaoDongID
        {
            get { return this._LoaiHopDongLaoDongID; }
            set { this._LoaiHopDongLaoDongID = value; PropertyHasChanged("LoaiHopDongLaoDongID"); }
        }

        private DateTime? _NgayKyHopDongLaoDong = null;
        public DateTime? NgayKyHopDongLaoDong
        {
            get { return this._NgayKyHopDongLaoDong; }
            set { this._NgayKyHopDongLaoDong = value; PropertyHasChanged("NgayKyHopDongLaoDong"); }
        }

        private DateTime? _NgayKetThucHopDongLaoDong = null;
        public DateTime? NgayKetThucHopDongLaoDong
        {
            get { return this._NgayKetThucHopDongLaoDong; }
            set { this._NgayKetThucHopDongLaoDong = value; PropertyHasChanged("NgayKetThucHopDongLaoDong"); }
        }

        private int? _NgachLuongID = null;
        public int? NgachLuongID
        {
            get { return this._NgachLuongID; }
            set { this._NgachLuongID = value; PropertyHasChanged("NgachLuongID"); }
        }

        private int? _BacLuongID = null;
        public int? BacLuongID
        {
            get { return this._BacLuongID; }
            set { this._BacLuongID = value; PropertyHasChanged("BacLuongID"); }
        }

        private DateTime? _NgayApDungLuong = null;
        public DateTime? NgayApDungLuong
        {
            get { return this._NgayApDungLuong; }
            set { this._NgayApDungLuong = value; PropertyHasChanged("NgayApDungLuong"); }
        }

        private double? _HeSoLuong = 0;
        public double? HeSoLuong
        {
            get { return this._HeSoLuong; }
            set { this._HeSoLuong = value; PropertyHasChanged("HeSoLuong"); }
        }

        private double? _LuongCoBan = 0;
        public double? LuongCoBan
        {
            get { return this._LuongCoBan; }
            set { this._LuongCoBan = value; PropertyHasChanged("LuongCoBan"); }
        }

        private double? _ThanhTien = 0;
        public double? ThanhTien
        {
            get { return this._ThanhTien; }
            set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
        }

        private double? _LuongBHXH = 0;
        public double? LuongBHXH
        {
            get { return this._LuongBHXH; }
            set { this._LuongBHXH = value; PropertyHasChanged("LuongBHXH"); }
        }

        private bool? _BHXH = false;
        public bool? BHXH
        {
            get { return this._BHXH; }
            set { this._BHXH = value; PropertyHasChanged("BHXH"); }
        }

        private bool? _BHYT = false;
        public bool? BHYT
        {
            get { return this._BHYT; }
            set { this._BHYT = value; PropertyHasChanged("BHYT"); }
        }

        private bool? _BHTN = false;
        public bool? BHTN
        {
            get { return this._BHTN; }
            set { this._BHTN = value; PropertyHasChanged("BHTN"); }
        }

        private int? _LoaiTaiKhoanID = null;
        public int? LoaiTaiKhoanID
        {
            get { return this._LoaiTaiKhoanID; }
            set { this._LoaiTaiKhoanID = value; PropertyHasChanged("LoaiTaiKhoanID"); }
        }

        private string _MatKhau = null;
        public string MatKhau
        {
            get { return this._MatKhau; }
            set { this._MatKhau = value; PropertyHasChanged("MatKhau"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _NhanVienID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newNhanVienID = -1;
        private NhanVienBO()
        {
            _NhanVienID = newNhanVienID--;
        }

        public NhanVienBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public NhanVienBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static NhanVienBO NewRootNhanVienBO()
        {
            return DataPortal.Create<NhanVienBO>(new RootCriteria());
        }

        internal static NhanVienBO NewChildNhanVienBO()
        {
            return DataPortal.Create<NhanVienBO>(new ChildCriteria());
        }

        public static NhanVienBO GetRootNhanVienBO(int _NhanVienID)
        {
            return DataPortal.Fetch<NhanVienBO>(new RootCriteria(_NhanVienID));
        }

        public static NhanVienBO GetRootNhanVienBOByWhere(string where)
        {
            return DataPortal.Fetch<NhanVienBO>(where);
        }

        internal static NhanVienBO GetChildNhanVienBO(SqlDataReader dataReader)
        {
            return new NhanVienBO(dataReader);
        }

        public static void DeleteNhanVienBO(int _NhanVienID)
        {
            DataPortal.Delete(new RootCriteria(_NhanVienID));
        }

        public static void DeleteNhanVienBO(int _NhanVienID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_NhanVienID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int NhanVienID;
            public bool directDelete = false;
            public RootCriteria(int _NhanVienID)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = false;
            }

            public RootCriteria(int _NhanVienID, bool directDelete)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int NhanVienID;
            public bool directDelete = false;
            public ChildCriteria(int _NhanVienID)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = false;
            }

            public ChildCriteria(int _NhanVienID, bool directDelete)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[NhanVienID] = " + criteria.NhanVienID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["NhanVienID"] is DBNull))
                this._NhanVienID = (int)dataReader["NhanVienID"];
            if (!(dataReader["MaNhanVien"] is DBNull))
                this._MaNhanVien = (string)dataReader["MaNhanVien"];
            if (!(dataReader["Anh"] is DBNull))
                this._Anh = (byte[])dataReader["Anh"];
            if (!(dataReader["Ho"] is DBNull))
                this._Ho = (string)dataReader["Ho"];
            if (!(dataReader["Ten"] is DBNull))
                this._Ten = (string)dataReader["Ten"];
            if (!(dataReader["Nu"] is DBNull))
                this._Nu = (bool)dataReader["Nu"];
            if (!(dataReader["NgaySinh"] is DBNull))
                this._NgaySinh = (DateTime)dataReader["NgaySinh"];
            if (!(dataReader["NoiSinh"] is DBNull))
                this._NoiSinh = (string)dataReader["NoiSinh"];
            if (!(dataReader["DiaChi"] is DBNull))
                this._DiaChi = (string)dataReader["DiaChi"];
            if (!(dataReader["NguyenQuan"] is DBNull))
                this._NguyenQuan = (string)dataReader["NguyenQuan"];
            if (!(dataReader["SoCMND"] is DBNull))
                this._SoCMND = (string)dataReader["SoCMND"];
            if (!(dataReader["NgayCapCMND"] is DBNull))
                this._NgayCapCMND = (DateTime)dataReader["NgayCapCMND"];
            if (!(dataReader["NoiCapCMND"] is DBNull))
                this._NoiCapCMND = (string)dataReader["NoiCapCMND"];
            if (!(dataReader["SoHoChieu"] is DBNull))
                this._SoHoChieu = (string)dataReader["SoHoChieu"];
            if (!(dataReader["NgayCapHoChieu"] is DBNull))
                this._NgayCapHoChieu = (DateTime)dataReader["NgayCapHoChieu"];
            if (!(dataReader["NgayHetHanHoChieu"] is DBNull))
                this._NgayHetHanHoChieu = (DateTime)dataReader["NgayHetHanHoChieu"];
            if (!(dataReader["Email"] is DBNull))
                this._Email = (string)dataReader["Email"];
            if (!(dataReader["DienThoai"] is DBNull))
                this._DienThoai = (string)dataReader["DienThoai"];
            if (!(dataReader["DanTocID"] is DBNull))
                this._DanTocID = (int)dataReader["DanTocID"];
            if (!(dataReader["TonGiaoID"] is DBNull))
                this._TonGiaoID = (int)dataReader["TonGiaoID"];
            if (!(dataReader["QuocTichID"] is DBNull))
                this._QuocTichID = (int)dataReader["QuocTichID"];
            if (!(dataReader["TrinhDoHocVanID"] is DBNull))
                this._TrinhDoHocVanID = (int)dataReader["TrinhDoHocVanID"];
            if (!(dataReader["TrinhDoNgoaiNguID"] is DBNull))
                this._TrinhDoNgoaiNguID = (int)dataReader["TrinhDoNgoaiNguID"];
            if (!(dataReader["TrinhDoTinHocID"] is DBNull))
                this._TrinhDoTinHocID = (int)dataReader["TrinhDoTinHocID"];
            if (!(dataReader["BangCapID"] is DBNull))
                this._BangCapID = (int)dataReader["BangCapID"];
            if (!(dataReader["ChuyenMonID"] is DBNull))
                this._ChuyenMonID = (int)dataReader["ChuyenMonID"];
            if (!(dataReader["NoiDaoTao"] is DBNull))
                this._NoiDaoTao = (string)dataReader["NoiDaoTao"];
            if (!(dataReader["PhongBanID"] is DBNull))
                this._PhongBanID = (int)dataReader["PhongBanID"];
            if (!(dataReader["ChucVuID"] is DBNull))
                this._ChucVuID = (int)dataReader["ChucVuID"];
            if (!(dataReader["TinhTrangCongViecID"] is DBNull))
                this._TinhTrangCongViecID = (int)dataReader["TinhTrangCongViecID"];
            if (!(dataReader["NgayBatDauLamViec"] is DBNull))
                this._NgayBatDauLamViec = (DateTime)dataReader["NgayBatDauLamViec"];
            if (!(dataReader["NgayKetThucLamViec"] is DBNull))
                this._NgayKetThucLamViec = (DateTime)dataReader["NgayKetThucLamViec"];
            if (!(dataReader["MaHopDongLaoDong"] is DBNull))
                this._MaHopDongLaoDong = (string)dataReader["MaHopDongLaoDong"];
            if (!(dataReader["LoaiHopDongLaoDongID"] is DBNull))
                this._LoaiHopDongLaoDongID = (int)dataReader["LoaiHopDongLaoDongID"];
            if (!(dataReader["NgayKyHopDongLaoDong"] is DBNull))
                this._NgayKyHopDongLaoDong = (DateTime)dataReader["NgayKyHopDongLaoDong"];
            if (!(dataReader["NgayKetThucHopDongLaoDong"] is DBNull))
                this._NgayKetThucHopDongLaoDong = (DateTime)dataReader["NgayKetThucHopDongLaoDong"];
            if (!(dataReader["NgachLuongID"] is DBNull))
                this._NgachLuongID = (int)dataReader["NgachLuongID"];
            if (!(dataReader["BacLuongID"] is DBNull))
                this._BacLuongID = (int)dataReader["BacLuongID"];
            if (!(dataReader["NgayApDungLuong"] is DBNull))
                this._NgayApDungLuong = (DateTime)dataReader["NgayApDungLuong"];
            if (!(dataReader["HeSoLuong"] is DBNull))
                this._HeSoLuong = (double)dataReader["HeSoLuong"];
            if (!(dataReader["LuongCoBan"] is DBNull))
                this._LuongCoBan = (double)dataReader["LuongCoBan"];
            if (!(dataReader["ThanhTien"] is DBNull))
                this._ThanhTien = (double)dataReader["ThanhTien"];
            if (!(dataReader["LuongBHXH"] is DBNull))
                this._LuongBHXH = (double)dataReader["LuongBHXH"];
            if (!(dataReader["BHXH"] is DBNull))
                this._BHXH = (bool)dataReader["BHXH"];
            if (!(dataReader["BHYT"] is DBNull))
                this._BHYT = (bool)dataReader["BHYT"];
            if (!(dataReader["BHTN"] is DBNull))
                this._BHTN = (bool)dataReader["BHTN"];
            if (!(dataReader["LoaiTaiKhoanID"] is DBNull))
                this._LoaiTaiKhoanID = (int)dataReader["LoaiTaiKhoanID"];
            if (!(dataReader["MatKhau"] is DBNull))
                this._MatKhau = (string)dataReader["MatKhau"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienBO_InsertBO");
            db.AddInParameter(cmd, "@MaNhanVien", SqlDbType.NVarChar, this._MaNhanVien);
            db.AddInParameter(cmd, "@Anh", SqlDbType.Image, this._Anh);
            db.AddInParameter(cmd, "@Ho", SqlDbType.NVarChar, this._Ho);
            db.AddInParameter(cmd, "@Ten", SqlDbType.NVarChar, this._Ten);
            db.AddInParameter(cmd, "@Nu", SqlDbType.Bit, this._Nu);
            db.AddInParameter(cmd, "@NgaySinh", SqlDbType.DateTime, this._NgaySinh);
            db.AddInParameter(cmd, "@NoiSinh", SqlDbType.NVarChar, this._NoiSinh);
            db.AddInParameter(cmd, "@DiaChi", SqlDbType.NVarChar, this._DiaChi);
            db.AddInParameter(cmd, "@NguyenQuan", SqlDbType.NVarChar, this._NguyenQuan);
            db.AddInParameter(cmd, "@SoCMND", SqlDbType.NVarChar, this._SoCMND);
            db.AddInParameter(cmd, "@NgayCapCMND", SqlDbType.DateTime, this._NgayCapCMND);
            db.AddInParameter(cmd, "@NoiCapCMND", SqlDbType.NVarChar, this._NoiCapCMND);
            db.AddInParameter(cmd, "@SoHoChieu", SqlDbType.NVarChar, this._SoHoChieu);
            db.AddInParameter(cmd, "@NgayCapHoChieu", SqlDbType.DateTime, this._NgayCapHoChieu);
            db.AddInParameter(cmd, "@NgayHetHanHoChieu", SqlDbType.DateTime, this._NgayHetHanHoChieu);
            db.AddInParameter(cmd, "@Email", SqlDbType.NVarChar, this._Email);
            db.AddInParameter(cmd, "@DienThoai", SqlDbType.NVarChar, this._DienThoai);
            db.AddInParameter(cmd, "@DanTocID", SqlDbType.Int, this._DanTocID);
            db.AddInParameter(cmd, "@TonGiaoID", SqlDbType.Int, this._TonGiaoID);
            db.AddInParameter(cmd, "@QuocTichID", SqlDbType.Int, this._QuocTichID);
            db.AddInParameter(cmd, "@TrinhDoHocVanID", SqlDbType.Int, this._TrinhDoHocVanID);
            db.AddInParameter(cmd, "@TrinhDoNgoaiNguID", SqlDbType.Int, this._TrinhDoNgoaiNguID);
            db.AddInParameter(cmd, "@TrinhDoTinHocID", SqlDbType.Int, this._TrinhDoTinHocID);
            db.AddInParameter(cmd, "@BangCapID", SqlDbType.Int, this._BangCapID);
            db.AddInParameter(cmd, "@ChuyenMonID", SqlDbType.Int, this._ChuyenMonID);
            db.AddInParameter(cmd, "@NoiDaoTao", SqlDbType.NVarChar, this._NoiDaoTao);
            db.AddInParameter(cmd, "@PhongBanID", SqlDbType.Int, this._PhongBanID);
            db.AddInParameter(cmd, "@ChucVuID", SqlDbType.Int, this._ChucVuID);
            db.AddInParameter(cmd, "@TinhTrangCongViecID", SqlDbType.Int, this._TinhTrangCongViecID);
            db.AddInParameter(cmd, "@NgayBatDauLamViec", SqlDbType.DateTime, this._NgayBatDauLamViec);
            db.AddInParameter(cmd, "@NgayKetThucLamViec", SqlDbType.DateTime, this._NgayKetThucLamViec);
            db.AddInParameter(cmd, "@MaHopDongLaoDong", SqlDbType.NVarChar, this._MaHopDongLaoDong);
            db.AddInParameter(cmd, "@LoaiHopDongLaoDongID", SqlDbType.Int, this._LoaiHopDongLaoDongID);
            db.AddInParameter(cmd, "@NgayKyHopDongLaoDong", SqlDbType.DateTime, this._NgayKyHopDongLaoDong);
            db.AddInParameter(cmd, "@NgayKetThucHopDongLaoDong", SqlDbType.DateTime, this._NgayKetThucHopDongLaoDong);
            db.AddInParameter(cmd, "@NgachLuongID", SqlDbType.Int, this._NgachLuongID);
            db.AddInParameter(cmd, "@BacLuongID", SqlDbType.Int, this._BacLuongID);
            db.AddInParameter(cmd, "@NgayApDungLuong", SqlDbType.DateTime, this._NgayApDungLuong);
            db.AddInParameter(cmd, "@HeSoLuong", SqlDbType.Float, this._HeSoLuong);
            db.AddInParameter(cmd, "@LuongCoBan", SqlDbType.Float, this._LuongCoBan);
            db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
            db.AddInParameter(cmd, "@LuongBHXH", SqlDbType.Float, this._LuongBHXH);
            db.AddInParameter(cmd, "@BHXH", SqlDbType.Bit, this._BHXH);
            db.AddInParameter(cmd, "@BHYT", SqlDbType.Bit, this._BHYT);
            db.AddInParameter(cmd, "@BHTN", SqlDbType.Bit, this._BHTN);
            db.AddInParameter(cmd, "@LoaiTaiKhoanID", SqlDbType.Int, this._LoaiTaiKhoanID);
            db.AddInParameter(cmd, "@MatKhau", SqlDbType.NVarChar, this._MatKhau);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewNhanVienID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._NhanVienID = (int)cmd.Parameters["@NewNhanVienID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienBO_UpdateBO");
            db.AddInParameter(cmd, "@NhanVienID", SqlDbType.Int, this._NhanVienID);
            db.AddInParameter(cmd, "@MaNhanVien", SqlDbType.NVarChar, this._MaNhanVien);
            db.AddInParameter(cmd, "@Anh", SqlDbType.Image, this._Anh);
            db.AddInParameter(cmd, "@Ho", SqlDbType.NVarChar, this._Ho);
            db.AddInParameter(cmd, "@Ten", SqlDbType.NVarChar, this._Ten);
            db.AddInParameter(cmd, "@Nu", SqlDbType.Bit, this._Nu);
            db.AddInParameter(cmd, "@NgaySinh", SqlDbType.DateTime, this._NgaySinh);
            db.AddInParameter(cmd, "@NoiSinh", SqlDbType.NVarChar, this._NoiSinh);
            db.AddInParameter(cmd, "@DiaChi", SqlDbType.NVarChar, this._DiaChi);
            db.AddInParameter(cmd, "@NguyenQuan", SqlDbType.NVarChar, this._NguyenQuan);
            db.AddInParameter(cmd, "@SoCMND", SqlDbType.NVarChar, this._SoCMND);
            db.AddInParameter(cmd, "@NgayCapCMND", SqlDbType.DateTime, this._NgayCapCMND);
            db.AddInParameter(cmd, "@NoiCapCMND", SqlDbType.NVarChar, this._NoiCapCMND);
            db.AddInParameter(cmd, "@SoHoChieu", SqlDbType.NVarChar, this._SoHoChieu);
            db.AddInParameter(cmd, "@NgayCapHoChieu", SqlDbType.DateTime, this._NgayCapHoChieu);
            db.AddInParameter(cmd, "@NgayHetHanHoChieu", SqlDbType.DateTime, this._NgayHetHanHoChieu);
            db.AddInParameter(cmd, "@Email", SqlDbType.NVarChar, this._Email);
            db.AddInParameter(cmd, "@DienThoai", SqlDbType.NVarChar, this._DienThoai);
            db.AddInParameter(cmd, "@DanTocID", SqlDbType.Int, this._DanTocID);
            db.AddInParameter(cmd, "@TonGiaoID", SqlDbType.Int, this._TonGiaoID);
            db.AddInParameter(cmd, "@QuocTichID", SqlDbType.Int, this._QuocTichID);
            db.AddInParameter(cmd, "@TrinhDoHocVanID", SqlDbType.Int, this._TrinhDoHocVanID);
            db.AddInParameter(cmd, "@TrinhDoNgoaiNguID", SqlDbType.Int, this._TrinhDoNgoaiNguID);
            db.AddInParameter(cmd, "@TrinhDoTinHocID", SqlDbType.Int, this._TrinhDoTinHocID);
            db.AddInParameter(cmd, "@BangCapID", SqlDbType.Int, this._BangCapID);
            db.AddInParameter(cmd, "@ChuyenMonID", SqlDbType.Int, this._ChuyenMonID);
            db.AddInParameter(cmd, "@NoiDaoTao", SqlDbType.NVarChar, this._NoiDaoTao);
            db.AddInParameter(cmd, "@PhongBanID", SqlDbType.Int, this._PhongBanID);
            db.AddInParameter(cmd, "@ChucVuID", SqlDbType.Int, this._ChucVuID);
            db.AddInParameter(cmd, "@TinhTrangCongViecID", SqlDbType.Int, this._TinhTrangCongViecID);
            db.AddInParameter(cmd, "@NgayBatDauLamViec", SqlDbType.DateTime, this._NgayBatDauLamViec);
            db.AddInParameter(cmd, "@NgayKetThucLamViec", SqlDbType.DateTime, this._NgayKetThucLamViec);
            db.AddInParameter(cmd, "@MaHopDongLaoDong", SqlDbType.NVarChar, this._MaHopDongLaoDong);
            db.AddInParameter(cmd, "@LoaiHopDongLaoDongID", SqlDbType.Int, this._LoaiHopDongLaoDongID);
            db.AddInParameter(cmd, "@NgayKyHopDongLaoDong", SqlDbType.DateTime, this._NgayKyHopDongLaoDong);
            db.AddInParameter(cmd, "@NgayKetThucHopDongLaoDong", SqlDbType.DateTime, this._NgayKetThucHopDongLaoDong);
            db.AddInParameter(cmd, "@NgachLuongID", SqlDbType.Int, this._NgachLuongID);
            db.AddInParameter(cmd, "@BacLuongID", SqlDbType.Int, this._BacLuongID);
            db.AddInParameter(cmd, "@NgayApDungLuong", SqlDbType.DateTime, this._NgayApDungLuong);
            db.AddInParameter(cmd, "@HeSoLuong", SqlDbType.Float, this._HeSoLuong);
            db.AddInParameter(cmd, "@LuongCoBan", SqlDbType.Float, this._LuongCoBan);
            db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
            db.AddInParameter(cmd, "@LuongBHXH", SqlDbType.Float, this._LuongBHXH);
            db.AddInParameter(cmd, "@BHXH", SqlDbType.Bit, this._BHXH);
            db.AddInParameter(cmd, "@BHYT", SqlDbType.Bit, this._BHYT);
            db.AddInParameter(cmd, "@BHTN", SqlDbType.Bit, this._BHTN);
            db.AddInParameter(cmd, "@LoaiTaiKhoanID", SqlDbType.Int, this._LoaiTaiKhoanID);
            db.AddInParameter(cmd, "@MatKhau", SqlDbType.NVarChar, this._MatKhau);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_NhanVienID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@NhanVienID", SqlDbType.Int, criteria.NhanVienID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_NhanVienID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
